{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当使用```make_experiment```创建实验时，您可以通过参数```reward_metric```重新指定评估指标，示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hyperts.experiment import make_experiment\n",
    "\n",
    "experiment = make_experiment(train_data, \n",
    "                             task='forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             reward_metric='mae',\n",
    "                             ...) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了传入内置支持的评估指标，您也可以自定义评估指标来满足特定场景下的需求，例如："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方式一："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "def custom_metric(y_true, y_pred, epsihon=1e-06):\n",
    "    if (y_true < 0).any():\n",
    "        y_true = np.clip(y_true, a_min=epsihon, a_max=abs(y_true))\n",
    "\n",
    "    if (y_pred < 0).any():\n",
    "        y_pred = np.clip(y_pred, a_min=epsihon, a_max=abs(y_pred))\n",
    "\n",
    "    return mean_squared_error(np.log1p(y_true), np.log1p(y_pred))\n",
    "\n",
    "\n",
    "experiment = make_experiment(train_data, \n",
    "                             task='forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             reward_metric=custom_metric,\n",
    "                             optimize_direction='min',\n",
    "                             ...) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意:** 当采用这种方式自定评估指标时，需指定优化方向```optimize_direction```。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方式二:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.metrics import mean_squared_error, make_scorer\n",
    "\n",
    "def custom_metric(y_true, y_pred, epsihon=1e-6):\n",
    "    if (y_true < 0).any():\n",
    "        y_true = np.clip(y_true, a_min=epsihon, a_max=abs(y_true))\n",
    "\n",
    "    if (y_pred < 0).any():\n",
    "        y_pred = np.clip(y_pred, a_min=epsihon, a_max=abs(y_pred))\n",
    "\n",
    "    return mean_squared_error(np.log1p(y_true), np.log1p(y_pred))\n",
    "\n",
    "custom_scorer = make_scorer(custom_metric, greater_is_better=True, needs_proba=False)\n",
    "\n",
    "experiment = make_experiment(train_data, \n",
    "                             task='forecast',\n",
    "                             timestamp='TimeStamp',\n",
    "                             reward_metric=custom_metric,\n",
    "                             scorer=make_scorer,\n",
    "                             ...) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意:** 当采用这种方式自定评估指标时，需设置参数```scorer```。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "29dd11bf16d40fe19950dcc2f06dd773fb6bc4491ac296fb211bfed7a4a532da"
  },
  "kernelspec": {
   "display_name": "Python 3.7.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.7.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
